//
//  WalletsEntity.m
//  HYApp
//
//  Created by Jianbo Cai on 2020/8/24.
//  Copyright © 2020 Unidax. All rights reserved.
//

#import "WalletsEntity.h"
#import "NSString+Extra.h"


@implementation BalanceListEntity

- (NSString *)imageName {
    if (!_imageName) {
        _imageName = self.asset_address;
    }
    return _imageName;
}

- (NSString *)assetDescribe {
    if (!_assetDescribe) {
//        NSArray *array = @[@"USDT", @"BTC", @"ETH", @"HKD", @"USD"];
//        NSArray *describeArray = @[@"/TetherUS", @"/Bitcoin", @"/Ethereum", @"/Virtual", @"/USD"];
        for (Currencie *entity in Currencies().currencieList) {
            if ([entity.display_symbol isEqualToString:self.asset_address]) {
                _assetDescribe = entity.token_name;
                break;
            }
        }
    }
    return _assetDescribe;
}

- (void)setAsset_address:(NSString *)asset_address {
    _asset_address = asset_address;
    if ([asset_address isEqualToString:@"HKD"] || [asset_address isEqualToString:@"USD"]) {
        self.actionType = FunctionActionTypeExchange | FunctionActionTypeTransfer;
    }else {
        self.actionType = FunctionActionTypeDeposit | FunctionActionTypeExchange | FunctionActionTypeWithdraw;
    }
}

@end


@implementation MetadataEntity
@end



@implementation ETHEntity

+ (NSDictionary *)modelContainerPropertyGenericClass {
    return @{
             @"balance" : [BalanceListEntity class],
             @"metadata" : MetadataEntity.class,
             };
}

- (NSString *)usdTotalValue {
    if (!_usdTotalValue) {
        double total = 0;
        for (BalanceListEntity *entity in self.balance) {
            total += entity.indicative_usd_equivalent.doubleValue;
        }
        _usdTotalValue = [NSString stringWithFormat:@"%.02f", total];
    }
    return _usdTotalValue;
}

- (NSString *)availableTotalValue {
    if (!_availableTotalValue) {
        double total = 0;
        for (BalanceListEntity *entity in self.balance) {
            total += entity.available_balance_display.doubleValue;
        }
        _availableTotalValue = [NSString stringWithFormat:@"%.02f", total];
    }
    return _availableTotalValue;
}

@end






@implementation WalletsEntity

WalletsEntity *Wallets() {
    return [WalletsEntity standardDefaults];
}

static WalletsEntity *wallets_defaults = nil;
+ (WalletsEntity *)standardDefaults {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        wallets_defaults = [[WalletsEntity alloc] init];
    });
    return wallets_defaults;
}

+ (NSDictionary *)modelContainerPropertyGenericClass {
    return @{
             @"eth" : [ETHEntity class],
             };
}

- (void)setLast_updated:(NSString *)last_updated {
    _last_updated = [last_updated timespToGMTFormat:@"yyyy-MM-dd HH:mm:ss"];
}

- (void)setEth:(NSArray<ETHEntity *> *)eth {
    _eth = [NSArray arrayWithArray:eth];
    for (ETHEntity *entity in eth) {
        if ([entity.metadata.jurisdiction isEqualToString:@"hk"]) {
            self.hkAccount = entity;
        }else if ([entity.metadata.jurisdiction isEqualToString:@"us"]) {
            self.usAccount = entity;
        }else if ([entity.metadata.jurisdiction isEqualToString:@"sg"]) {
            self.sgAccount = entity;
        }else if ([entity.metadata.type isEqualToString:@"digital"]) {
            self.digital = entity;
        }
    }
}

@end
